/**
 * 
 */
package travibot.connection.cookies;

import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @author tajzivit
 *
 */
public class Cookie {

    private static final String NAME = "NAME";
    private static final String EXPIRES = "expires";
    private static final String DOMAIN = "domain";
    private static final String PATH = "path";
    private static final String SECURE = "secure";
    
    private static final String HEADER_FORMAT = "{0}={1};";
    
    private String name;
    private String value;
    private String host;
    private String path;
    private boolean secure;
    private Date expires;
    
    private SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, DD-EEE-yyyy HH:MM:SS");
    
    public Cookie(String nonParsedCookie){
        
        try {
            parse(nonParsedCookie);
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
    }
    
    public String toString(){
        
        StringBuffer result = new StringBuffer();
        
        result.append(getFormattedHeaderPart(name, value));
        
        if(expires != null)
            result.append(getFormattedHeaderPart(EXPIRES, dateFormat.format(expires)));
        
        if(host != null)
            result.append(getFormattedHeaderPart(DOMAIN, host));
        
        if(path != null)
            result.append(getFormattedHeaderPart(PATH, path));

        result.append(getFormattedHeaderPart(SECURE, Boolean.toString(secure)));
        
        return result.toString();
    }
    
    private void parse(String nonParsedCookie) throws ParseException{
        
        String[] parsedCookie = nonParsedCookie.split(";");
        for(int i=0; i < parsedCookie.length; i++){
            
            String part = parsedCookie[i].trim();
            String[] parsedCookiePart = parseCookiePart(part);
            
            if(parsedCookiePart[0].equals(EXPIRES)){
                this.expires = dateFormat.parse(parsedCookiePart[1]);
            }
            else if(parsedCookiePart[0].equals(DOMAIN)){
                this.host = parsedCookiePart[1];
            }
            else if(parsedCookiePart[0].equals(PATH)){
                this.path = parsedCookiePart[1];
            }
            else if(parsedCookiePart[0].equals(SECURE)){
                this.secure = Boolean.valueOf(parsedCookiePart[1]).booleanValue();
            }
            else {
                this.name = parsedCookiePart[0];
                this.value = parsedCookiePart[1];
            }
        }
    }
    
    private String[] parseCookiePart(String cookiePart){
        
        return cookiePart.split("=");
    }
    
    private String getFormattedHeaderPart(String headerName, String value){
        
        return MessageFormat.format(HEADER_FORMAT, new Object[]{headerName, value});
    }
}
